<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>角色管理</title>
    <link rel="shortcut icon" href="favicon.ico">
    <link href="css/bootstrap.min14ed.css?v=3.3.6" rel="stylesheet">
    <link href="css/font-awesome.min93e3.css?v=4.4.0" rel="stylesheet">
    <link href="css/animate.min.css" rel="stylesheet">
    <link href="css/style.min862f.css?v=4.1.0" rel="stylesheet">
    <link href="css/style.winning.css?v=1.0.0" rel="stylesheet">
    <link rel="stylesheet" href="css/metroStyle/metroStyle.css" type="text/css">
    <link rel="stylesheet" href="css/plugins/table/bootstrap-table.css" type="text/css">
    <link rel="stylesheet" href="css/demo.css?v=1.0.0" type="text/css">

</head>

<body class="gray-bg">
<div class="wrapper wrapper-content animated fadeIn">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox">
                <div class="ibox-content">
                    <div class="row">

                        <!--右侧开始-->
                        <div class="col-sm-6">
                            <div class="panel panel-default">
                                <div class="panel-body">
                                    <div class="col-sm-10">
                                        <ul class="nav nav-tabs">
                                            <li class="active"><a href="#tab-1" data-toggle="tab">功能权限设置</a>
                                            </li>
                                            <li class=""><a href="#tab-2" data-toggle="tab">数据权限设置</a></li>
                                        </ul>
                                    </div>
                                </div>
                                <div>

                                </div>
                                <div class="panel-body">
                                    <div class="tab-content">
                                        <div class="tab-pane active" id="tab-1">
                                            <button class="btn  btn-primary btn-xs" type="button"
                                                    onclick="saveGnqxRole()">保存功能权限设置
                                            </button>
                                            <div class="content_wrap">
                                                <div class="zTreeDemoBackground left">
                                                    <div id="tree_gnqx" class="ztree"></div>
                                                </div>

                                            </div>
                                        </div>
                                        <div class="tab-pane" id="tab-2">
                                            <button class="btn  btn-primary btn-xs" type="button"
                                                    onclick="saveSjqxRole()">保存数据权限设置
                                            </button>
                                            <div class="content_wrap">
                                                <div class="zTreeDemoBackground left">
                                                    <div id="tree_sjqx" class="ztree"></div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <!--  按钮开始-->

                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript" src="js/jquery.min.js?v=3.5.1"></script>
<script type="text/javascript" src="js/bootstrap.min.js?v=3.3.6"></script>
<script type="text/javascript" src="js/content.min.js?v=1.0.0"></script>
<script type="text/javascript" src="js/jquery.ztree.core.js?v=3.5"></script>
<script type="text/javascript" src="js/jquery.ztree.excheck.js?v=3.5"></script>
<script type="text/javascript" src="js/plugins/jsTree/jstree.min.js"></script>
<script type="text/javascript" src="js/plugins/table/bootstrap-table.js"></script>
<script type="text/javascript" src="js/plugins/table/locale/bootstrap-table-zh-CN.js"></script>
<script type="text/javascript" src="js/plugins/layer/layer.min.js"></script>
<script type="text/javascript" src="platcommon/common.js"></script>
<script type="text/javascript" src="platcommon/layer-wn.js"></script>
<script>
    $(document).ready(function () {
        // $.fn.zTree.init($("#tree_gnqx"), setting_gnqx, zNodes_gnqx);
        // $.fn.zTree.init($("#tree_sjqx"), setting_sjqx, zNodes_sjqx);
        getGnqxTree();

    });

    //****************机构权限设置 树形 start
    var setting_gnqx = {
        check: {
            enable: true
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        view: {
            showIcon: false
        }
    };

    var setting_sjqx = {
        check: {
            enable: true
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        view: {
            showIcon: false
        }
    };

    var zNodes_gnqx = [];
    var zNodes_sjqx = [];
    //****************机构权限设置 树形end
    //****************角色权限设置 树形 start

    //****************角色权限设置 树形end
    //***************************ztree 点击样式 *************
    var newCount = 1;
    function addHoverDom(treeId, treeNode) {
        var sObj = $("#" + treeNode.tId + "_span");
        if (treeNode.editNameFlag || $("#addBtn_" + treeNode.tId).length > 0) return;
        var addStr = "<span class='button add' id='addBtn_" + treeNode.tId + "' title='add node' onfocus='this.blur();'></span>";
        sObj.after(addStr);
        var btn = $("#addBtn_" + treeNode.tId);
        if (btn) btn.bind("click",
            function () {
                var zTree = $.fn.zTree.getZTreeObj("tree_jg");
                zTree.addNodes(treeNode, {
                    id: (100 + newCount),
                    pId: treeNode.id,
                    name: "new node" + (newCount++)
                });
                return false;
            });
    };
    function removeHoverDom(treeId, treeNode) {
        $("#addBtn_" + treeNode.tId).unbind().remove();
    };

    //***************************ztree 点击样式 *************


    /**
     * 获取菜单树--功能权限使用-数据权限使用
     */
    function getGnqxTree() {
        // alert("jgbm=="+Common.GetRequest().jgbm);
        // alert("jsdm=="+Common.GetRequest().jsdm);

        if(Common.GetRequest().jgbm!=undefined && Common.GetRequest().jgbm !=""){
            $.ajax({
                type: "GET",
                contentType: 'application/json',
                dataType: "json",
                url: "role/getmenutree/" + Common.GetRequest().jgbm,
                success: function (jsonData) {
                    if (jsonData.code == 200) {
                        $.fn.zTree.init($("#tree_gnqx"), setting_gnqx, jsonData.menuTreeList);
                        $.fn.zTree.init($("#tree_sjqx"), setting_sjqx, jsonData.menuTreeList);
                        getGnqxCheck();
                        getSjqxCheck();
                    } else {
                    }
                }
            });
        }


    }

    //保存功能权限设置
    function saveGnqxRole() {

        if (validate(onCheckGnqxZtree())) {
            $.ajax({
                type: "POST",
                contentType: 'application/x-www-form-urlencoded',
                dataType: "json",
                url: "role/savegnqx",
                data: {
                    ztreeCheckList: onCheckGnqxZtree(),
                    jgbm: Common.GetRequest().jgbm,
                    jsdm: Common.GetRequest().jsdm
                },
                success: function (jsonData) {
                    if (jsonData.code == 200) {   //查询成功
                        LayerWn.showSucAlert("保存成功");
                        LayerWn.closeDialog();

                    }
                }
            });
        }

    }

    //获取功能权限 选中
    function onCheckGnqxZtree() {
        var treeObj = $.fn.zTree.getZTreeObj("tree_gnqx"),
            nodes = treeObj.getCheckedNodes(true),
            v = "";
        for (var i = 0; i < nodes.length; i++) {
            v += nodes[i].id + ",";
        }
        return v;

    }


    //提交前校验
    function validate(ztreelist) {


        if (ztreelist.length == 0) {
            LayerWn.showAlert('请选择权限！');
            return false;
        }
        return true;
    }


    //获取数据权限 选中
    function onCheckSJqxZtree() {
        var treeObj = $.fn.zTree.getZTreeObj("tree_sjqx"),
            nodes = treeObj.getCheckedNodes(true),
            v = "";
        for (var i = 0; i < nodes.length; i++) {
            v += nodes[i].id + ",";
        }
        return v;

    }

    //保存数据权限设置
    function saveSjqxRole() {

        if (validate(onCheckSJqxZtree())) {
            $.ajax({
                type: "POST",
                contentType: 'application/x-www-form-urlencoded',
                dataType: "json",
                url: "role/savesjqx",
                data: {
                    ztreeCheckList: onCheckSJqxZtree(),
                    jgbm: Common.GetRequest().jgbm,
                    jsdm: Common.GetRequest().jsdm
                },
                success: function (jsonData) {
                    if (jsonData.code == 200) {   //查询成功
                        LayerWn.showSucAlert("保存成功");
                        LayerWn.closeDialog();

                    }
                }
            });
        }

    }

    //获取功能权限已有数据
    function getGnqxCheck() {
        $.ajax({
            type: "GET",
            contentType: 'application/json',
            dataType: "json",
            url: "role/getGnqxCheck",
            data: {
                jgbm: Common.GetRequest().jgbm,
                jsdm: Common.GetRequest().jsdm
            },
            success: function (jsonData) {
                if (jsonData.code == 200) {   //查询成功

                    var list = jsonData.sysGnqxCheckList;
                    var treeObj = $.fn.zTree.getZTreeObj("tree_gnqx");
                    if (list.length > 0) {
                        for (var i = 0; i < list.length; i++) {
                            var data = list[i];
                            var node = treeObj.getNodeByParam("id", data.cddm, null);
                            treeObj.selectNode(node);
                            treeObj.expandNode(node, true, false, false);
                            treeObj.checkNode(node, true, false);
                        }
                    }
                }
            }
        });

    }
    //获取数据权限 已有数据
    function getSjqxCheck() {
        $.ajax({
            type: "GET",
            contentType: 'application/json',
            dataType: "json",
            url: "role/getSjqxCheck",
            data: {
                jgbm: Common.GetRequest().jgbm,
                jsdm: Common.GetRequest().jsdm
            },
            success: function (jsonData) {
                if (jsonData.code == 200) {   //查询成功

                    var list = jsonData.sysSjqxCheckList;
                    var treeObj = $.fn.zTree.getZTreeObj("tree_sjqx");
                    if (list.length > 0) {
                        for (var i = 0; i < list.length; i++) {
                            var data = list[i];
                            var node = treeObj.getNodeByParam("id", data.gnsjdm, null);
                            treeObj.selectNode(node);
                            treeObj.expandNode(node, true, false, false);
                            treeObj.checkNode(node, true, false);
                        }
                    }

                }
            }
        });


    }

</script>
</body>

</html>